<?php
/**
 * biotiwai
 * 体外筛选
 * @author Administrator
 * @version
 */
class Model_Tables_Biotiwai extends Zend_Db_Table_Abstract {
    /**
     * The default table name
     */
    protected $_name = 'biotiwai';
    /**
     * add
     * 增加体外筛选
     * @param $params 字段信息
     */
    public function add( $params )
    {
        $data = array(
		'compoundid' => $params['compoundid'],
        'compoundno' => $params['compoundno'],
        'projectid' => $params['projectid'],
		'dict2' => $params['dict2'],
		'dict4' => $params['dict4'],
		'dict3' => $params['dict3'],
        'dict7' => $params['dict7'],
        'dict8' => $params['dict8'],
		'v' => $params['v'],
		'dict1' => $params['dict1'],
		'notebookid' => $params['notebookid'],
		'page' => $params['page'],
		'expdate' => $params['expdate'],
		'createman' => $params['createman'],
        );
        return $this->insert($data);
    }
    /**
     * modify
     * 编辑体外筛选
     * @param $params 字段信息
     * @param $id 主键值
     */
    public function modify( $params, $id )
    {
        $data = array(
		//'compoundid' => $params['compoundid'],
		'dict2' => $params['dict2'],
		'dict4' => $params['dict4'],
		'dict3' => $params['dict3'],
        'dict7' => $params['dict7'],
        'dict8' => $params['dict8'],
		'v' => $params['v'],
		'dict1' => $params['dict1'],
		'notebookid' => $params['notebookid'],
		'page' => $params['page'],
		'expdate' => $params['expdate'],
		'createman' => $params['createman'],
        );
        $where = 'id = '.$id ;
        return $this->update($data , $where );
    }
    /**
     * modifyAjax
     * 无刷新编辑体外筛选
     * @param $id 主键值
     * @param $field 字段
     * @param $value 值
     */
    public function modifyAjax( $id, $field, $value )
    {
    	$db = $this->getAdapter();
    	$data = array($field => $value);
    	$where = $db->quoteInto("id=?", $id);
        return $this->update($data , $where );
    }
    /**
     * modifyAjaxRevers
     * 无刷新字段反转体外筛选
     * @param $field 需要反转的字段
     * @param $id 主键值
     */
    public function modifyAjaxRevers( $field ,$pri)
    {
    	$db = $this->getAdapter();
    	$sql = "update biotiwai set  $field =not  $field  where id=' $pri'";
		return $db->query($sql);
    }
    /**
     * del
     * 根据条件删除体外筛选
     * @param $id 主键值
     */
    public function del( $id )
    {
     	$where = 'id = '.$id;
        return $this->delete($where);
    }
    /**
     * getInfo
     * 获取体外筛选信息
     * @param $id 主键值
     * @return 结果数组
     */
    public function getInfo($id)
    {
        $row = $this->fetchRow('id = ' . $id);
        if (!$row) {
        	return null;
        }
        else
        {
        	return $row->toArray();
        }
    }
    public function getInfoBycompoundno($compoundno){
    	$sql = "select count(*) as num from biotiwai where compoundno=:compoundno";
    	$result = $this->getAdapter()->fetchOne($sql,array("compoundno"=>$compoundno));
    	return $result;
    }
    /**
     * getCount
     * 获取体外筛选数量
     * @param int  搜索条件
     * @return 结果数
     */
    public function getCount($s = "")
    { 
    	$db = $this->getAdapter();
    	if($s!="")
    	{
    		$where  = $db->quoteInto(" where biotiwai.compoundno like?", "%$s%");
    		$where .= $db->quoteInto(" or project.projectid like?", "%$s%");
    		$where .= $db->quoteInto(" or d1.name like?", "%$s%");
    		$where .= $db->quoteInto(" or d2.name like?", "%$s%");
    	}
    	$select = "select count(*) as num from biotiwai 
			left join project on biotiwai.projectid = project.id
			left join dictionaryitems d1 on biotiwai.dict7 = d1.id 
			left join dictionaryitems d2 on biotiwai.dict8 = d2.id $where";
    	//echo $select;
    	$result =  $db->fetchRow($select)->num;
    	return $result;
        /*$select = $this->select()->from($this->_name,'count(*) as num');
    	if($s!="")  
    	{
    		$db = $this->getAdapter();
			$where = $db->quoteInto("compoundno=?", $s);
    		$select = $select->where($where);
    	}
        $result = $this->fetchRow($select)->num;
        return $result;*/
    }
    /**
     * getList
     * 取体外筛选数据分页
     * @param int  页码
     * @param int  每页的行数 
     * @param  搜索条件
     * @return 行数组
     */
    public function getList($page, $numPerPage = 10,$s= "")
    {
    	$db = $this->getAdapter();
    	$page = (1>$page) ? 1 : $page;
    	$offset = ($page-1)*$numPerPage;
    	$orderby = " order by id ";
    	$limit = " limit $offset, $numPerPage";
    	if($s!="")
    	{
    		$where  = $db->quoteInto(" where biotiwai.compoundno like?", "%$s%");
    		$where .= $db->quoteInto(" or project.projectid like?", "%$s%");
    		$where .= $db->quoteInto(" or d1.name like?", "%$s%");
    		$where .= $db->quoteInto(" or d2.name like?", "%$s%");
    	}
    	$select = "select biotiwai.* from biotiwai
    	left join project on biotiwai.projectid = project.id
    	left join dictionaryitems d1 on biotiwai.dict7 = d1.id
    	left join dictionaryitems d2 on biotiwai.dict8 = d2.id  $where $orderby $limit";
    	//echo $select;exit;
    	$result =  $db->fetchAll($select);
    	return $result;
    	
    	/*$page = (1>$page) ? 1 : $page;
    	$offset = ($page-1)*$numPerPage;
        $orderby = array('id');
        if($s!="")     
		{
			$db = $this->getAdapter();
			$where = $db->quoteInto("compoundno= ?", $s);
		}  
        $result = $this->fetchAll($where,$orderby, $numPerPage, $offset )->toArray();
        return $result;*/
    }
}